using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;
using System.Linq;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_GMMould : UIBaseNode
    {

        #region Template Generate,don't modify
        protected partial class UIB_CMDMould
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnConfirm { protected set; get; }
            public IQIGame.Onigao.Framework.ExInputField inputCmdValue { protected set; get; }
            public IQIGame.Onigao.Framework.ExText iD { protected set; get; }
            public IQIGame.Onigao.Framework.ExText title { protected set; get; }
            public IQIGame.Onigao.Framework.ExText cmd { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnConfirm", out var __tbv0);
                this.btnConfirm = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExInputField>("inputCmdValue", out var __tbv1);
                this.inputCmdValue = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("iD", out var __tbv2);
                this.iD = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("title", out var __tbv3);
                this.title = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("cmd", out var __tbv4);
                this.cmd = __tbv4;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify


        protected UIB_CMDMould ui { get; set; }
        private const int MaxHistoryEntries = 30; //最多保存30条历史记录

        protected override void BeforeInit()
        {
            ui = new UIB_CMDMould();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.btnConfirm.onClick.AddListener(this.OnClickConfirm);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {

        }

        UD_GMCmd uiData = null;
        public void Show(UD_GMCmd data)
        {
            uiData = data;
            this.ui.btnConfirm.SetActive(uiData != null);
            if (uiData == null)
                return;

            this.Show();
            this.ui.iD.text = uiData.cid.ToString();
            this.ui.title.text = uiData.name;
            this.ui.cmd.text = uiData.cmd;
            this.ui.inputCmdValue.text = uiData.cmdValue;
        }

        void OnClickConfirm()
        {
            var Args = this.ui.inputCmdValue.text.Trim();
            string cmd = uiData.cmd;
            string cmdArgs = Args;
            (this.belongWindow as UIGMList).SendCmd(cmd, cmdArgs);
            AddHistory(cmd, cmdArgs);

        }
        private void AddHistory(string cmd, string cmdArgs)
        {
            var historyLog = new PlayerPreferenceString(PlayerPrefsConst.GMHistory, string.Empty);
            //historyLog.Value = "";
            string currentLog = historyLog.Value;
            string[] logEntries = currentLog.Split('\n').Where(s => !string.IsNullOrEmpty(s)).ToArray();
            if (logEntries.Length == MaxHistoryEntries)
            {
                logEntries[logEntries.Length - 1] = "";
                currentLog = string.Join("\n", logEntries);
            }
            if (cmd == "exec")
            { currentLog = cmd + "|" + DateTime.Now.ToString() + "|" + cmdArgs + "\n" + currentLog; }
            else
            {
                currentLog = cmd + "|" + DateTime.Now.ToString() + "|" + uiData.name + "|" + cmdArgs + "\n" + currentLog;
            }
            historyLog.Value = currentLog;
        }
    }
}
